// https://www.lintcode.com/problem/meeting-rooms/description?_from=cat

/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param intervals: an array of meeting time intervals
     * @return: if a person could attend all meetings
     */
     
    // 要先排序
    void sort(vector<Interval> &intervals)
    {
        for (int i = intervals.size() - 1; i >= 0; --i)
        {
            for (int j = i; j >= 0; --j)
            {
                if (intervals[i].start < intervals[j].start)
                {
                    Interval tmp = intervals[i];
                    intervals[i] = intervals[j];
                    intervals[j] = tmp;
                }
            }
        }
    }
    bool canAttendMeetings(vector<Interval> &intervals) {
// Input
// []
// Expected
// true
        int begin = INT_MAX;
        int end = INT_MIN;
        sort(intervals);
        for (int i = 0; i < intervals.size(); ++i)
        {
            if (intervals[i].start < end && intervals[i].end > begin)
            {
                return false;
            }
            begin = min(begin, intervals[i].start);
            end = max(end, intervals[i].end);
        }
        return true;
    }
};